package com.coffee.admin.service.supplier.impl;

import com.coffee.admin.api.supplier.query.SupplierRoleRelationQuery;
import com.coffee.admin.api.supplier.result.SupplierRoleRelationResult;
import com.coffee.admin.api.supplier.service.SupplierRoleRelationService;
import com.coffee.admin.service.supplier.bean.SupplierRoleRelation;
import com.coffee.admin.service.supplier.dao.SupplierRoleRelationDao;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.StringUtil;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.*;


@DubboService(dynamic = true)
public class SupplierRoleRelationServiceImpl implements SupplierRoleRelationService {
	@Resource
	private SupplierRoleRelationDao	supplierRoleRelationDao;

	@Override
	public Page<SupplierRoleRelationResult> getPager(SupplierRoleRelationQuery param, PagerInfo pr) {
		Page<SupplierRoleRelationResult> pager=new Page<SupplierRoleRelationResult>(pr.getStart(), pr.getPageSize());
		Integer count=supplierRoleRelationDao.count(param);
		pager.setTotalPage(count);
		if(count>0) {
			List<SupplierRoleRelation> list=supplierRoleRelationDao.page(param, pr.getStart(), pr.getPageSize());
			pager.setData(DTOUtils.convertList(list, SupplierRoleRelationResult.class));
		}
		return pager;
	}
	
	@Override
	public Integer count(SupplierRoleRelationQuery param){
		Integer count=supplierRoleRelationDao.count(param);
		return count;
	}
	
	@Override
	public SupplierRoleRelationResult getDetail(Integer id){
		SupplierRoleRelation item=supplierRoleRelationDao.findById(id);
		if(item==null){
			return null;
		}
		SupplierRoleRelationResult result=new SupplierRoleRelationResult();
		BeanUtilExt.copyProperties(result, item);
		
		return result;
	}

	@Override
	public List<SupplierRoleRelationResult> getList(SupplierRoleRelationQuery param, PagerInfo pr){
		List<SupplierRoleRelation> list=supplierRoleRelationDao.page(param, pr.getStart(), pr.getPageSize());
		return DTOUtils.convertList(list, SupplierRoleRelationResult.class);
	}
	
	@Override
	public List<SupplierRoleRelationResult> getList(SupplierRoleRelationQuery param){
		List<SupplierRoleRelation> list=supplierRoleRelationDao.page(param, 0, Integer.MAX_VALUE);
		return DTOUtils.convertList(list, SupplierRoleRelationResult.class);
	}
	
	@Override
	public List<SupplierRoleRelationResult> getList(){
		SupplierRoleRelationQuery param = new SupplierRoleRelationQuery();
		List<SupplierRoleRelation> list=supplierRoleRelationDao.page(param, 0, Integer.MAX_VALUE);
		return DTOUtils.convertList(list, SupplierRoleRelationResult.class);
	}


	@Override
	public void delete(Integer id) {
		supplierRoleRelationDao.deleteById(id);
	}

	@Override
	public void addOrUpdate(SupplierRoleRelationResult result)throws BusinessException {
		SupplierRoleRelation item=new SupplierRoleRelation();
		BeanUtilExt.copyProperties(item, result);
		if(item.getId()==null) {
			item.setCreateTime(new Date());
			supplierRoleRelationDao.insert(item);
		}else {
			SupplierRoleRelation tmp=supplierRoleRelationDao.findById(item.getId());
			if(tmp==null) {
				throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
			}
			supplierRoleRelationDao.updateById(item);
		}
	}
	
	@Override
	public Map<Integer,SupplierRoleRelationResult> getItemMap(Set<Integer> idSet){
		Map<Integer, SupplierRoleRelationResult> map=new HashMap<Integer, SupplierRoleRelationResult>();
		List<SupplierRoleRelation> list=supplierRoleRelationDao.findByIds(StringUtil.setToList(idSet));
		List<SupplierRoleRelationResult> resultList=DTOUtils.convertList(list, SupplierRoleRelationResult.class);
		for(SupplierRoleRelationResult item:resultList) {
			map.put(item.getId(), item);
		}
		return map;
	}
	
	@Override
	public Map<Integer,SupplierRoleRelationResult> getItemMap(){
		Map<Integer, SupplierRoleRelationResult> map=new HashMap<Integer, SupplierRoleRelationResult>();
		SupplierRoleRelationQuery param=new SupplierRoleRelationQuery();
		List<SupplierRoleRelation> list=supplierRoleRelationDao.page(param, 0, Integer.MAX_VALUE);
		List<SupplierRoleRelationResult> resultList=DTOUtils.convertList(list, SupplierRoleRelationResult.class);
		for(SupplierRoleRelationResult item:resultList) {
			map.put(item.getId(), item);
		}
		return map;
	}	
	
	
	
}
